package erp_services

import (
	"encoding/json"
	"log"
	"xiehui-service/app/common/request"
	"xiehui-service/global"
	"xiehui-service/utils"

	"go.uber.org/zap"
)

const (
	LqOrderApi  = "/sdlq/lqapi/dd4"    // 订单
	LqPhbApi    = "/sdlq/lqapi/phb"    // 配合比
	LqWzdApi    = "/sdlq/lqapi/wzadd"  // 物资消耗
	LqGbdApi    = "/sdlq/lqapi/gbd"    // 过磅单
	LqFhdAddApi = "/sdlq/lqapi/fhdadd" // 生成发货单

	LqSccjPhbApi = "/sdlq/lqybapi/sccjphb" // 生产厂家配合比查询接口
	LqZdfhApi    = "/sdlq/lqybapi/zdfh"    // 自动发货
	LqWzxhApi    = "/sdlq/lqybapi/wzxh"    // 物资消耗
	LqQrwcddApi  = "/sdlq/lqapi/qrwcdd"    // 已确认完成的订单查询
	LqGxfhslApi  = "/sdlq/lqybapi/gxfhsl"  // 更新发货数量
)

type lqApiService struct {
}

// 路桥接口对接服务
var LqApiService = new(lqApiService)

// 获取路桥的订单
func (lqApiService *lqApiService) HttpOrder(args request.OrderRequest) (ret ApiResponse, err error) {
	headers := GenHeaders()
	// 构建请求Body体
	requestBody, err := json.Marshal(args)
	if err != nil {
		global.App.Log.Error("[80]获取路桥订单:", zap.Any("参数解析失败", err.Error()))
		return
	}

	url := global.App.Config.Erp.AppUrl + LqOrderApi
	err = utils.HttpHandler.GenericHTTPCallWithHeaders("POST", url, requestBody, headers, &ret)
	if err != nil {
		// 处理错误
		log.Println("[80]获取路桥订单数据失败", err)
		global.App.Log.Info("[80]获取路桥订单数据失败:", zap.Any("原因", err))
	} else {
		log.Println("[80]获取路桥订单数据成功", string(requestBody))
		global.App.Log.Info("[80]获取路桥订单数据成功:", zap.Any("数据", string(requestBody)), zap.Any("Res", ret))
	}

	return
}

// 获取路桥的配比
func (lqApiService *lqApiService) HttpPhb(args request.PhbRequest) (ret ApiResponse, err error) {
	headers := GenHeaders()
	var phbReq request.PhbRequest
	phbReq.CfgcGCID = "" //global.App.Config.App.Zdid

	// 构建请求Body体
	requestBody, err := json.Marshal(args)
	if err != nil {
		global.App.Log.Error("[81]获取路桥配比接口:", zap.Any("参数解析失败", err.Error()))
		return
	}

	url := global.App.Config.Erp.AppUrl + LqPhbApi
	err = utils.HttpHandler.GenericHTTPCallWithHeaders("POST", url, requestBody, headers, &ret)
	if err != nil {
		// 处理错误
		log.Println("[81]获取路桥配比数据失败", err)
		global.App.Log.Info("[81]获取路桥配比数据失败:", zap.Any("原因", err))
	} else {
		log.Println("[81]获取路桥配比数据成功", "入参:", string(requestBody), "条数:", len(ret.Data.([]interface{})))
		global.App.Log.Info("[81]获取路桥配比数据成功:", zap.Any("入参", string(requestBody)), zap.Any("数据条数", len(ret.Data.([]interface{}))))
	}

	return
}

// 将博硕生产系统的耗料上传至平台
func (lqApiService *lqApiService) HttpUpConsumption(requestBody []byte) (ret ApiResponse, err error) {
	headers := GenHeaders()
	url := global.App.Config.Erp.AppUrl + LqWzdApi
	err = utils.HttpHandler.GenericHTTPCallWithHeaders("POST", url, requestBody, headers, &ret)
	if err != nil {
		// 处理错误
		log.Println("[82]上传耗料数据失败", err)
		global.App.Log.Info("[82]上传耗料数据失败:", zap.Any("原因", err))
	} else {
		log.Println("[82]上传耗料数据成功", ret, string(requestBody))
		global.App.Log.Info("[82]上传耗料数据成功:", zap.Any("数据", string(requestBody)), zap.Any("Res", ret))
	}

	return
}

// 路桥发货接口
func (lqApiService *lqApiService) HttpUpFhd(requestBody []byte) (ret ApiResponse, err error) {
	headers := GenHeaders()
	url := global.App.Config.Erp.AppUrl + LqFhdAddApi
	err = utils.HttpHandler.GenericHTTPCallWithHeaders("POST", url, requestBody, headers, &ret)
	if err != nil {
		// 处理错误
		log.Println("[83]路桥发货数据失败", err)
		global.App.Log.Info("[83]路桥发货数据失败:", zap.Any("原因", err))
	} else {
		log.Println("[83]路桥发货数据成功", ret, string(requestBody))
		global.App.Log.Info("[83]路桥发货数据成功:", zap.Any("数据", string(requestBody)), zap.Any("Res", ret))
	}

	return
}

// 获取路桥的过磅单
func (lqApiService *lqApiService) HttpGbd(args request.GbdRequest) (ret ApiResponse, err error) {
	headers := GenHeaders()
	// 构建请求Body体
	requestBody, err := json.Marshal(args)
	if err != nil {
		global.App.Log.Error("[84]获取路桥磅单:", zap.Any("参数解析失败", err.Error()))
		return
	}

	url := global.App.Config.Erp.AppUrl + LqGbdApi
	err = utils.HttpHandler.GenericHTTPCallWithHeaders("POST", url, requestBody, headers, &ret)
	if err != nil {
		// 处理错误
		log.Println("[84]获取路桥磅单数据失败", err)
		global.App.Log.Info("[84]获取路桥磅单数据失败:", zap.Any("原因", err))
	} else {
		log.Println("[84]获取路桥磅单数据成功", ret, string(requestBody))
		global.App.Log.Info("[84]获取路桥磅单数据成功:", zap.Any("数据", string(requestBody)), zap.Any("Res", ret))
	}

	return
}

// 生产厂家配合比查询接口
func (lqApiService *lqApiService) HttpSccjPhb(args request.SccjPhbRequest) (ret ApiResponse, err error) {
	headers := GenHeaders()
	// 构建请求Body体
	requestBody, err := json.Marshal(args)
	if err != nil {
		global.App.Log.Error("[85]获取生产厂家配合比:", zap.Any("参数解析失败", err.Error()))
		return
	}

	url := global.App.Config.Erp.AppUrl + LqSccjPhbApi
	err = utils.HttpHandler.GenericHTTPCallWithHeaders("POST", url, requestBody, headers, &ret)
	if err != nil {
		// 处理错误
		log.Println("[85]获取生产厂家配合比数据失败", err)
		global.App.Log.Info("[85]获取生产厂家配合比数据失败:", zap.Any("原因", err))
		return
	} else {
		log.Println("[85]获取生产厂家配合比数据成功", ret, string(requestBody))
		global.App.Log.Info("[85]获取生产厂家配合比数据成功:", zap.Any("数据", string(requestBody)), zap.Any("Res", ret))
	}

	return
}

// 自动发货
func (lqApiService *lqApiService) HttpZdfh(requestBody []byte) (ret ApiResponse, err error) {
	headers := GenHeaders()
	url := global.App.Config.Erp.AppUrl + LqZdfhApi
	err = utils.HttpHandler.GenericHTTPCallWithHeaders("POST", url, requestBody, headers, &ret)

	if err != nil {
		// 处理错误
		log.Println("[86]自动发货失败", err)
		global.App.Log.Info("[86]自动发货失败:", zap.Any("原因", err))
		return
	} else {
		log.Println("[86]自动发货成功", ret, string(requestBody))
		global.App.Log.Info("[86]自动发货成功:", zap.Any("数据", string(requestBody)), zap.Any("Res", ret))
	}

	return
}

// 物资消耗
func (lqApiService *lqApiService) HttpWzxh(requestBody []byte) (ret ApiResponse, err error) {
	headers := GenHeaders()
	url := global.App.Config.Erp.AppUrl + LqWzxhApi
	err = utils.HttpHandler.GenericHTTPCallWithHeaders("POST", url, requestBody, headers, &ret)

	if err != nil {
		//处理错误
		log.Println("[87]物资消耗失败", err)
		global.App.Log.Info("[87]物资消耗失败:", zap.Any("原因", err))
		return
	} else {
		log.Println("[87]物资消耗成功", ret, string(requestBody))
		global.App.Log.Info("[87]物资消耗成功:", zap.Any("数据", string(requestBody)), zap.Any("Res", ret))
	}
	return
}

// 已确认完成订单查询
func (lqApiService *lqApiService) HttpQrwcdd(args request.OrderRequest) (ret ApiResponse, err error) {
	headers := GenHeaders()

	// 构建请求Body体
	requestBody, err := json.Marshal(args)
	if err != nil {
		global.App.Log.Error("[88]获取已确认完成订单:", zap.Any("参数解析失败", err.Error()))
		return
	}

	url := global.App.Config.Erp.AppUrl + LqQrwcddApi
	err = utils.HttpHandler.GenericHTTPCallWithHeaders("POST", url, requestBody, headers, &ret)

	if err != nil {
		//处理错误
		log.Println("[88]已确认完成订单查询失败", err)
		global.App.Log.Info("[88]已确认完成订单查询失败:", zap.Any("原因", err))
		return
	} else {
		log.Println("[88]已确认完成订单查询成功", ret, string(requestBody))
		global.App.Log.Info("[88]已确认完成订单查询成功:", zap.Any("数据", string(requestBody)), zap.Any("Res", ret))
	}
	return
}

// 更新发货数量
func (lqApiService *lqApiService) HttpGxfhsl(requestBody []byte) (ret ApiResponse, err error) {
	headers := GenHeaders()
	url := global.App.Config.Erp.AppUrl + LqGxfhslApi
	err = utils.HttpHandler.GenericHTTPCallWithHeaders("POST", url, requestBody, headers, &ret)

	if err != nil {
		// 处理错误
		log.Println("[89]更新发货数量失败", err)
		global.App.Log.Info("[89]更新发货数量失败:", zap.Any("原因", err))
		return
	} else {
		log.Println("[89]更新发货数量成功", ret, string(requestBody))
		global.App.Log.Info("[86]更新发货数量成功:", zap.Any("数据", string(requestBody)), zap.Any("Res", ret))
	}

	return
}

// /////////\\\\\\\\\\\\\
func GenHeaders() map[string]string {
	headers := make(map[string]string)
	headers[global.App.Config.Erp.Username] = global.App.Config.Erp.Pwd

	return headers
}
